package main

import (
	"fmt"
	"zinx/ziface"
	"zinx/znet"
)

// ping test 自定义路由
type PingRouter struct {
	znet.BaseRouter
}

// 模版方法设计模式
// Test PreHandle
func (r *PingRouter) PreHandle(request ziface.IRequest) {
	fmt.Println("Call Router PreHandle ....")
	_, err := request.GetConnection().GetTCPConnection().Write([]byte("\nbefore ping...\n"))
	if err != nil {
		fmt.Println("call back before ping error", err)
	}
}

// Test Handle
func (r *PingRouter) Handle(request ziface.IRequest) {
	fmt.Println("Call Router Handle ....")
	_, err := request.GetConnection().GetTCPConnection().Write([]byte("ping... ping...\n"))
	if err != nil {
		fmt.Println("call back  ping....ping error", err)
	}
}

// Test Post Handle
func (r *PingRouter) PostHandle(request ziface.IRequest) {
	fmt.Println("Call Router PostHandle ....")
	_, err := request.GetConnection().GetTCPConnection().Write([]byte("after... ping...\n"))
	if err != nil {
		fmt.Println("call back  after..ping error", err)
	}
}
func main() {
	s := znet.NewServer("[zinx V0.3]")

	// 添加路由
	s.AddRouter(&PingRouter{})
	s.Serve()
}
